package edu.tum.in.campar.twodui.studentcanteen;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.ImageButton;
import android.widget.TextView;
import android.view.GestureDetector.SimpleOnGestureListener;

public class DVDateAndMealActivity extends SLMealDetailActivity implements OnTouchListener{
	private GestureDetector gestureDetector; 
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.listview);
		super.onCreate(savedInstanceState);
		
		gestureDetector = new GestureDetector(this, new SimpleOnGestureListener() {
			@Override
			public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
					float velocityY) {
				float dX = e1.getX() - e2.getX();
				
				if (Math.abs(dX) < 40)
					return false;
				
				if (dX > 0) // swipe left
				{
					setDate(+1);
					return true;
				}
				
				setDate(-1);
				return true;
			}
		});
		this.getListView().setOnTouchListener(this);
		
		((TextView)findViewById(R.id.date_text)).setText(menu.getDay());
		
		((ImageButton)findViewById(R.id.date_next)).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				setDate(+1);
			}
		});
		if (index == menus.size()-1)
			((ImageButton)findViewById(R.id.date_next)).setVisibility(View.GONE);
		
		((ImageButton)findViewById(R.id.date_prev)).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				setDate(-1);
			}
		});
		if (index == 0)
			((ImageButton)findViewById(R.id.date_prev)).setVisibility(View.GONE);
		
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		if (gestureDetector != null && gestureDetector.onTouchEvent(event))
			return true;
		
		return false;
	}
	
	private void setDate(int offset) {
		index += offset;
		
		if (index < 0 || index > menus.size()-1)
			return;
		
		Intent i = new Intent(DVDateAndMealActivity.this, DVDateAndMealActivity.class);
		i.putExtra("day", index);
		startActivity(i);
		if (offset > 0)
			overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_left);
		else
			overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_right);
		finish();
		
	}
}
